package com.dzsw.test.jxls;

import lombok.extern.slf4j.Slf4j;
import org.jxls.common.Context;
import org.jxls.util.JxlsHelper;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class JxlsDemo {

    public static void main(String[] args) {
        test_data_4();
    }


    public static void test_data_4() {
        log.info("Running Object Collection demo data four.");
        List<DataFour> dataList = generateData4();
        String src = "E:\\data\\desktop\\客户协议图片\\银盛配置模板\\template_4.xlsx";
        String target = "E:\\data\\desktop\\客户协议图片\\银盛配置模板\\result_4.xlsx";
        try (InputStream is = new FileInputStream(src)) {
            try (OutputStream os = new FileOutputStream(target)) {
                Context context = new Context();
                context.putVar("dataList", dataList);
                JxlsHelper.getInstance().processTemplate(is, os, context);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void test_data_6() {
        log.info("Running Object Collection demo data six.");
        List<DataSix> dataList = generateData6();
        String src = "E:\\data\\desktop\\客户协议图片\\银盛配置模板\\template_6.xlsx";
        String target = "E:\\data\\desktop\\客户协议图片\\银盛配置模板\\result_6.xlsx";
        try (InputStream is = new FileInputStream(src)) {
            try (OutputStream os = new FileOutputStream(target)) {
                Context context = new Context();
                context.putVar("dataList", dataList);
                JxlsHelper.getInstance().processTemplate(is, os, context);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static List<DataSix> generateData6() {
        List<DataSix> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(new DataSix(
                    "custom_" + i,
                    "licence_" + i,
                    "XXX_USER_CODE_" + i
            ));
        }
        return list;
    }


    private static List<DataFour> generateData4() {
        List<DataFour> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(new DataFour(
                    "code_" + i,
                    "legal_name_" + i,
                    "legal_cert_no_" + i,
                    "phone_" + i,
                    "address_" + i,
                    "licence_number_" + i
            ));
        }
        return list;
    }
}
